<template>
  <div>
    <h2>添 加 病 害</h2>
    <div class="add">
      <div class="left">
        <el-form :label-position="labelPosition" label-width="80px">
          <el-form-item label="名称">
            <el-input v-model="mouseName"></el-input>
          </el-form-item>
          <el-form-item label="繁殖">
            <el-input v-model="mouseMultiply"></el-input>
          </el-form-item>

          <el-form-item label="图片">
            <el-upload
              class="avatar-uploader"
              action="/api/pest/upload2"
              :http-request="testUpload"
              :show-file-list="false"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

          <el-form-item label="防治措施">
            <el-input v-model="mouseControl"></el-input>
          </el-form-item>
        </el-form>
      </div>

      <div>
        <el-form :label-position="labelPosition" label-width="80px">
          <el-form-item label="食物">
            <el-input v-model="mouseFood"></el-input>
          </el-form-item>
          <el-form-item label="天敌">
            <el-input v-model="mouseSentinel"></el-input>
          </el-form-item>
          <el-form-item label="主要危害">
            <el-input v-model="mouseMainHarm"></el-input>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <el-row>
      <el-button plain @click="addMouse()">添加</el-button>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      mouseName: "",
      mouseFood: "",
      mouseMultiply: "",
      mouseSentinel: "",
      mousePic: "",
      mouseControl: "",
      mouseMainHarm: "",
      labelPosition: "left",
      imageUrl: "",
    };
  },
  methods: {
    async testUpload(item) {
      this.mousePic = await this.ajax.uploadSubmit("/api/pest/upload2", {
        file: item.file,
      });
      //console.log(this.pestLarvaPic);
      this.imageUrl = "/api/image/" + this.mousePic;
      this.mousePic = "/image/" + this.mousePic;
      console.log(this.mousePic);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    async addMouse() {
      let paramObj = {
        mouseName: this.mouseName,
        mouseFood: this.mouseFood,
        mouseMultiply: this.mouseMultiply,
        mouseSentinel: this.mouseSentinel,
        mousePic: this.mousePic,
        mouseControl: this.mouseControl,
        mouseMainHarm: this.mouseMainHarm,
      };
      let info = await this.ajax.getSubmit("/api/mouse/add", paramObj);
      console.log(info);

      this.mouseName = "";
      this.mouseFood = "";
      this.mouseMultiply = "";
      this.mouseSentinel = "";
      this.mousePic = "";
      this.mouseControl = "";
      this.mouseMainHarm = "";
    },
  },
};
</script>

<style>
</style>